﻿using System;
using UnityEngine;
using System.Collections;

public class Basics4 : MonoBehaviour
{
	private Vector3 lastPostion;
	
	private void Update()
	{
		if (Network.isServer)
		{
			//Only the server can move the cube!
			Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"),
				0f, Input.GetAxis("Vertical"));
			float speed = 30f;
			transform.Translate(speed * moveDirection * Time.deltaTime);

			//Save some network bandwidth; only send an rpc
			//when the position has moved more than X
			if (Vector3.Distance(transform.position, lastPostion) >= 0.05)
			{
				lastPostion = transform.position;
				//Send the position Vector3 over to the others;
				//in this case all clients
				networkView.RPC("SetPosition", RPCMode.Others,
					transform.position);
			}
		}
	}


	[RPC]
	void SetPosition(Vector3 newPos)
	{
		//This RPC is in this case always called by the server,
		// but executed on all clients
		transform.position = newPos;
	}
}